package lin.an.food.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lin.an.food.entity.FoodDetails;
import lin.an.food.mapper.FoodDetailsMapper;
import lin.an.food.service.FoodDetailsService;
import lin.an.food.service.FoodFavoriteService;
import lin.an.food.service.FoodLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.ObjectUtil;

/**
 * @author zhoulinq
 * @date 2025/4/10
 */
@Service
public class FoodDetailsServiceImpl extends ServiceImpl<FoodDetailsMapper, FoodDetails> implements FoodDetailsService {
    @Autowired
    private FoodLikeService foodLikeService;
    @Autowired
    private FoodFavoriteService foodFavoriteService;

    @Override
    public FoodDetails getFoodDetail(int foodId) {
        LambdaQueryWrapper<FoodDetails> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FoodDetails::getFoodId, foodId);
        FoodDetails foodDetail = this.getOne(queryWrapper);
        if (ObjectUtil.isNull(foodDetail)) {
            throw new RuntimeException("未找到对应的美食详情");
        }
        return foodDetail;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean toggleLike(int foodId, int userId) {
        // 查询是否已点赞
        boolean isLiked = foodLikeService.isLiked(foodId, userId);

        // 更新点赞表
        if (isLiked) {
            // 已点赞，取消点赞
            foodLikeService.removeLike(foodId, userId);
            // 更新美食详情表中的点赞数
            this.update()
                    .setSql("likes = likes - 1")
                    .eq("food_id", foodId)
                    .update();
        } else {
            // 未点赞，添加点赞
            foodLikeService.addLike(foodId, userId);
            // 更新美食详情表中的点赞数
            this.update()
                    .setSql("likes = likes + 1")
                    .eq("food_id", foodId)
                    .update();
        }

        return !isLiked; // 返回新的点赞状态
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean toggleFavorite(int foodId, int userId) {
        
        // 查询是否已收藏
        boolean isFavorited = foodFavoriteService.isFavorited(foodId, userId);

        // 更新收藏表
        if (isFavorited) {
            // 已收藏，取消收藏
            foodFavoriteService.removeFavorite(foodId, userId);
            // 更新美食详情表中的收藏数
            this.update()
                    .setSql("favorites = favorites - 1")
                    .eq("food_id", foodId)
                    .update();
        } else {
            // 未收藏，添加收藏
            foodFavoriteService.addFavorite(foodId, userId);
            // 更新美食详情表中的收藏数
            this.update()
                    .setSql("favorites = favorites + 1")
                    .eq("food_id", foodId)
                    .update();
        }

        return !isFavorited; // 返回新的收藏状态
    }
}
